package com.ridbor001.demo.servlet;

import com.ridbor001.demo.bean.RDBGrade;
import com.ridbor001.demo.bean.RDBUser;
import com.ridbor001.demo.service.RDBGradeService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Optional;

@WebServlet("/editGrade")
public class RDBEditGradeServlet extends HttpServlet {

    RDBGradeService gradeService = new RDBGradeService();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session=req.getSession();
        RDBUser user=(RDBUser)session.getAttribute("user");
        String gradeId=req.getParameter("gradeId");

        if(!user.isTeacher()){
            resp.sendError(HttpServletResponse.SC_FORBIDDEN,"无权限");
            return;
        }

        Optional<RDBGrade>target = null;
        try {
            target = gradeService.getGradeById(gradeId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        if(target.isPresent()){
            RDBGrade grade=target.get();
            grade.setDailyScore(Double.parseDouble(req.getParameter("dailyScore")));
            grade.setStageScore(Double.parseDouble(req.getParameter("stageScore")));
            grade.setFinalScore(Double.parseDouble(req.getParameter("finalScore")));
            try {
                // 关键步骤：更新数据库
                gradeService.updateGrade(grade);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            resp.sendRedirect("/gradeManage?searchKey= ");
        }
        else{
            resp.sendError(HttpServletResponse.SC_NOT_FOUND,"未找到成绩");
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        HttpSession session=req.getSession();
        RDBUser user=(RDBUser)session.getAttribute("user");
        if(user.isTeacher()){
            String gradeId = req.getParameter("gradeId");
            if (gradeId == null || gradeId.isEmpty()) {
                resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "缺失gradeId参数");
                return;
            }

            req.setAttribute("gradeId",gradeId);

            Optional<RDBGrade> gradeOpt = null;
            try {
                gradeOpt = gradeService.getGradeById(gradeId);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }

            if (gradeOpt.isPresent()) {
                RDBGrade grade = gradeOpt.get();
                String studentId = grade.getStudentId();
                String courseId = grade.getCourseId();
                // 4. 设置请求属性
                req.setAttribute("grade", grade);
                req.setAttribute("studentId", studentId);
                req.setAttribute("courseId", courseId);

                req.getRequestDispatcher("/rDBEditGradeForm.jsp").forward(req, resp);

        }
        else{
                resp.sendError(HttpServletResponse.SC_NOT_FOUND, "成绩记录不存在");
        }
        }
        else{
            resp.sendError(HttpServletResponse.SC_FORBIDDEN, "无权限操作");
        }
    }
}
